'use strict';

angular.module('com.lx.ngsl.user.controller', [])
	/**
	 * 列表页面controller
	 */
  .controller('userListController', ['$scope', '$http', '$location', '$alert', 'userService', function($scope, $http, $location, $alert, userService) {
	$scope.items = [];
	$scope.maxSize = 5;
	$scope.pagesize = 5;
	$scope.totalItems = userService.totalItems;
	$scope.currentPage = userService.currentPage;
	
	
	//获取角色id与名称的对应关系
	$scope.roleMap = function() {//d
		$http.get(
				"/ngsl/v0/role/roleMap", {}).success(
						function(data, status) {
							userService.roleMap = data;
							$scope.roleMap = userService.roleMap;
						}).error(function(data, status) {
							alert(data.error);
							$scope.data = data || "Request failed";
							$scope.status = status;
						});
	};
	$scope.roleMap();
	
	
	
	
	$scope.pageChanged = function() {
		$scope.query($scope.currentPage);
	};
	$scope.searchItem = userService.searchItem;
	$scope.selectedUserId = userService.selectedUserId;
	
	//查询
	$scope.query = function(page) {//d
		page = page ? (page-1) : 0;
		var username = $scope.searchItem.username;
		$http({
			method:"GET",
			url:"/ngsl/v0/users",
			params:{
				"page":page,
				"size":$scope.pagesize,
				"sort":"id,asc",
				"username":username
			}
		}).success(
				function(data, status) {
					$scope.items = data.content;
					$scope.totalItems = data.totalElements;
				}).error(function(data, status) {
  					alert(data.error);
					$scope.data = data || "Request failed";
					$scope.status = status;
		});
	};
	
	//新增
	$scope.addUser = function() {
		userService.searchItem = $scope.searchItem;
		$location.path('/user/add');
	}
	
	//编辑
	$scope.editUser = function(item) {
		userService.editItem = item;
		userService.totalItems = $scope.totalItems;
		userService.currentPage = $scope.currentPage;
		userService.searchItem = $scope.searchItem;
		$location.path('/user/edit');
	};
	
	//删除
	$scope.delUser = function(item) {
		var id = item.id;
		$http({
			method : 'delete',
			url : '/ngsl/v0/user/'+id,
		}).success(function(data, status) {
			//alertTip("删除成功！", "success", "tip_div");
			$alert.tip("操作成功!");
			$scope.query($scope.currentPage);
		}).error(function(data, status) {
			alert(data.error);
			$scope.data = data || "Request failed";
			$scope.status = status;
		});
	}
	
	//重置密码
	$scope.resetPwd = function(item) {
		var id = item.id;
		$http({
			method : 'patch',
			url : '/ngsl/v0/users/'+ id +'/password',
		}).success(function(data, status) {
			$alert.tip("密码已重置!");
			$scope.query($scope.currentPage);
		}).error(function(data, status) {
			alert(data.error);
			$scope.data = data || "Request failed";
			$scope.status = status;
		});
	}
	
	
	//锁定账户
	$scope.lockUser = function(item) {
		var id = item.id;
		$http({
			method : 'patch',
			url : '/ngsl/v0/users/'+ id +'/status',
		}).success(function(data, status) {
			$alert.tip("操作成功!");
			$scope.query($scope.currentPage);
		}).error(function(data, status) {
			alert(data.error);
			$scope.data = data || "Request failed";
			$scope.status = status;
		});
	}
	//获取角色下拉列表树
	$scope.roleDropdownTree = function() {
		$http.get(
				"/ngsl/v0/role/dropdowntree", {}).success(
						function(data, status) {
							userService.roleTreeData = data;
						}).error(function(data, status) {
							alert(data.error);
							$scope.data = data || "Request failed";
							$scope.status = status;
						});
	};
	$scope.roleDropdownTree();
	
	$scope.clear = function(){
		$scope.searchItem = {
				username:""
		};
	}
	
	$scope.query($scope.currentPage);
	
  }])
	/**
	 * 新增页面controller
	 */
  .controller('userAddController', ['$scope', '$http', '$location', '$alert', 'userService', function($scope, $http, $location, $alert, userService) {
	  	$scope.submitted = false;
	  	$scope.addItem = {};
	  	$scope.addItem.accountState = '1';
	  
	    //角色下拉树设置初始值
	    $scope.roleTreeData = userService.roleTreeData;
	    $scope.selectedRole = $scope.roleTreeData[0];
	    
	    
		$scope.submitted = false;
		// 新增保存
		$scope.saveAddUser = function() {
			if ($scope.addForm.$valid) {
				$scope.addItem.roleId = $scope.selectedRole.id;
				$http({
					method : 'post',
					url : '/ngsl/v0/user',
					data : $scope.addItem,
				}).success(
						function(data, status) {
							if(data == false){
								$alert.error({message:"该用户名已存在！"});
							}else{
								$alert.tip("操作成功!");
								$scope.backToList();
							}
						}).error(function(data, status) {
		  					alert(data.error);
							$scope.data = data || "Request failed";
							$scope.status = status;
				});
			} else {
				$scope.submitted = true;
			}
		}
		
		$scope.backToList = function() {
			$location.path('/user/list');
		};
		
  }])
  	/**
	 * 编辑页面controller
	 */
  .controller('userEditController', ['$scope', '$http', '$location', '$alert', 'userService', function($scope, $http, $location, $alert, userService) {
		$scope.editItem = userService.editItem;
		$scope.submitted = false;
		 //角色下拉树设置初始值
	    $scope.roleTreeData = userService.roleTreeData;
	    $scope.selectedRole = $scope.roleTreeData[0];
		//获取角色id与名称的对应关系
		$scope.roleMap = function() {
			$http.get(
					"/ngsl/v0/role/roleMap", {}).success(
							function(data, status) {
								userService.roleMap = data;
								$scope.roleMap = userService.roleMap;
							}).error(function(data, status) {
								alert(data.error);
								$scope.data = data || "Request failed";
								$scope.status = status;
							});
		};
		
		$scope.roleMap();
		$scope.backToList = function() {
			$location.path('/user/list');
		}
		
		// 保存修改
		$scope.save = function() {
			if ($scope.editForm.$valid) {
				$scope.editForm.roleId = $scope.selectedRole.id;
				$http({
					method : 'put',
					url : '/ngsl/v0/user',
					data : $scope.editItem,
				}).success(
						function(data, status) {
							if(data == false){
								$alert.error({message:"该用户名已存在！"});
							}else{
								$alert.tip("操作成功!");
								$scope.backToList();
							}
						}).error(function(data, status) {
		  					alert(data.error);
							$scope.data = data || "Request failed";
							$scope.status = status;
				});
			} else {
				$scope.submitted = true;
			}
		}
		
		
		
  }])
  	







